package Test4;

import java.math.BigDecimal;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;

;

/*
 * 1,有一个水果销售摊位,销售3种水果,重量和单价各不相同,实现多次的销售业务 
 * 2,销售时如果为顾客为女性销售金额打8折 
 * 3,显示当前各种水果的库存数
 * 4,查询全部销售记录信息 
 * 5,加入其它水果品种 
 * 6,添加进货单 
 * 7,查询指定日期间隔的销售纯利润 
 * 8,每日17: :00之后为5折特价
 * 9,按销售数量排序显示前三名 10,示当前各种水果的库存数,成本单价,单价,累计销售数量/金额,累计进货数量/金额
 */
public class Funcation {

	static Scanner scanner = new Scanner(System.in);
	static JDBCUile jdbcUile = new JDBCUile();

	// 保留double 类型的两位小数
	public static double Change(double shu) {
		BigDecimal df = new BigDecimal(shu);
		double f1 = df.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

		return f1;
	}

	// 添加水果信息
	public static void addFruits() {

		while (true) {
			Fruits fruit = new Fruits();

			System.out.println("请输入名称");
			String name = scanner.nextLine();
			fruit.setName(name);
			System.out.println("请输入单价");
			double sellingPrice = Double.parseDouble(scanner.nextLine());
			fruit.setSellingPrice(sellingPrice);
			System.out.println("请输入成本价");
			double costPrice = Double.parseDouble(scanner.nextLine());
			fruit.setCostPrice(costPrice);
			System.out.println("请输入库存");
			double repertory = Double.parseDouble(scanner.nextLine());
			fruit.setRepertory(repertory);
			jdbcUile.insertFruits(fruit);

			double total = Change(costPrice) * Change(repertory);
			PurchesList purchesList = new PurchesList();
			purchesList.setName(name);
			purchesList.setAmount(repertory);
			purchesList.setCostPrice(costPrice);
			purchesList.setTotal(total);
			purchesList.setPrice(sellingPrice);

			jdbcUile.insertPurches(purchesList); // 累计进货量

			System.out.println("是否要继续增加(yes/no?)");
			String flag = scanner.nextLine();

			if ("no".equals(flag)) {
				break;
			}
		}

	}

	// 销售水果
	public static void sellingFruits() throws ParseException {

		while (true) {
			Fruits fruit = new Fruits();
			double total = 0.0;
			System.out.println("请输入时间 (yyyy-MM-dd-HH)");
			String time = scanner.nextLine();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH");

			Date date = sdf.parse(time);

			int hour = date.getHours();
			System.out.println(hour);

			System.out.println("请输入水果名称");
			String name = scanner.nextLine();

			fruit = jdbcUile.selectFruits(name);

			double sellingPrice = fruit.getSellingPrice();
			double costPrice = fruit.getCostPrice();
			double repertory = fruit.getRepertory();
			double cost = 0;
			double profit = 0;
			boolean flagamount = false;
			if (repertory > 0) {

				System.out.println("请输入重量");
				double weight = Double.parseDouble(scanner.nextLine());

				if (weight < repertory) {
					flagamount = true; // 判断数量

					System.out.println("请输入是否为女性，是请输入‘1’，否请输入‘0’");
					int judgesex = Integer.parseInt(scanner.nextLine());
					if (hour >= 17) {
						if (judgesex == 0) {
							cost = weight * sellingPrice * 0.5; // 计算花费
						} else {

							cost = weight * sellingPrice * 0.8 * 0.5;
						}
						total = Change(cost);
						System.out.println("消费为:" + total);

					} else {
						if (judgesex == 0) {
							cost = weight * fruit.getCostPrice(); // 计算花费
						} else {
							cost = weight * fruit.getCostPrice() * 0.8;
						}
						total = Change(cost);
						System.out.println("消费为:" + total);

					}
					profit = Change((sellingPrice - costPrice) * weight); // 计算净利润

					Record record = new Record();

					record.setName(name);
					record.setProfit(profit);
										
					record.setWeight(weight);
					record.setTime(time);
					jdbcUile.insertRecord(record);
					
					// 增加销售记录

					jdbcUile.updateFuits(name, (repertory - weight)); // 库存减少

				} else {
					System.out.println("库存不足");
				}

			} else {
				System.out.println("对不起，没有该水果了");
			}

			System.out.println("是否要继续购买，是请输入'1'否请输入'0'");
			int flag = Integer.parseInt(scanner.nextLine());
			if (flag == 0) {
				break;
			}
		}

	}

	// 显示所有消费信息
	public static void showAllRecords() throws Exception {
		ArrayList<Record> records = new ArrayList<>();
		records = jdbcUile.selectRecords();
		if (records != null) {
			for (Record record : records) {
				String name = record.getName();
				double weight = record.getWeight();
				double profit = record.getProfit();
				String time = record.getTime();

				System.out.println(name + "在" + time + "时间段内" + "售出了" + weight + "斤");
			}
		} else {
			System.out.println("没有记录");
		}
	}

	// 显示当前各种水果的库存数
	public static void showFruitsRepertory() {

		ArrayList<Fruits> fruits = jdbcUile.selectAllFruits();
		int i = 1;
		if (fruits != null) {
			for (Fruits fruit : fruits) {
				String name = fruit.getName();
				double repertory = fruit.getRepertory();
				System.out.println(i + " " + name + "的库存为：" + repertory + "斤");
				i++;
			}
		} else {
			System.out.println("没有记录");
		}
	}

	// 添加进货单
	public static void addPurchesList() {

		PurchesList purchesList = new PurchesList();
		System.out.println("请输入水果名称");
		String name = scanner.nextLine();
		System.out.println("请输入重量");
		double weight = Double.parseDouble(scanner.nextLine());
		System.out.println("请输入成本价");
		double costPrice = Double.parseDouble(scanner.nextLine());
		System.out.println("请输入销售价");
		double sellingPrice = Double.parseDouble(scanner.nextLine());

		double total = Change(costPrice) * Change(weight);

		purchesList.setName(name);
		purchesList.setAmount(weight);
		purchesList.setCostPrice(costPrice);
		purchesList.setTotal(total);
		purchesList.setPrice(sellingPrice);

		jdbcUile.insertPurches(purchesList);

		Fruits fruit = new Fruits();

		fruit = jdbcUile.selectFruits(name);
		double repertory = fruit.getRepertory();

		if (fruit.getName() == null) {
			fruit.setName(name);
			fruit.setSellingPrice(sellingPrice);
			fruit.setCostPrice(costPrice);
			fruit.setRepertory(weight);

			jdbcUile.insertFruits(fruit);

		} else {
			fruit.setName(name);
			fruit.setRepertory(repertory + weight);

		}

	}

	// 按照销量显示前三名
	public static void showTopThree() {

		ArrayList<Record> records = jdbcUile.selectAllRecords();

//		TreeSet<Fruits> fruits2 = new TreeSet<>();
//		if (fruits != null) {
//			for (Fruits fruit : fruits) {
//				fruits2.add(fruit);
//			}
//	}
		int a = 3;
		for (int i = 0; i < records.size() - 1; i++) {

			if (a == 0) {
				break;
			}
			Record record = new Record();
			record = records.get(i);
			String name = record.getName();
			double sum = record.getWeight();

			System.out.println(i + 1 + " " + name + " 的销量为" + sum);
			if (sum != records.get(i + 1).getWeight()) {
				a--;
			}

		}
	}

	// 7,查询指定日期间隔的销售纯利润
	public static void appointProfit(){ 

		System.out.println("请输入开始时间段 (yyyy-MM-dd-hh)");
		String time1 = scanner.nextLine(); 
		System.out.println("请输入结束时间段 (yyyy-MM-dd-hh)");
		String time2 = scanner.nextLine();
		double profit = jdbcUile.selectAllRecordsBytime(time1);

		System.out.println(time1 + "时到" + time2 + "时，之间的纯利润为：" + profit);
		

	}

	public static void showMenu() {

		System.out.println("---------	1.添加水果信息			  -----------");
		System.out.println("---------	2. 销售水果  			  -----------");
		System.out.println("---------	3.显示所有消费信息      	  -----------");
		System.out.println("---------	4.显示当前各种水果的库存数    -----------");
		System.out.println("---------	6.输出销量前三的           	  -----------");
		System.out.println("---------	7.查询某时间段的纯利润(yyyy-MM-dd HH)	--");
		System.out.println("---------	#.退出         			  -----------");
	}

	public static void chooseMenu() throws Exception {

		while (true) {
			showMenu();
			System.out.println("请输入标号进行功能选择");
			int choose = Integer.parseInt(scanner.nextLine());
			switch (choose) {

			case 1: {
				addFruits();
				break;
			}
			case 2: {
				sellingFruits();
				break;
			}
			case 3: {
				showAllRecords();
				break;
			}
			case 4: {
				showFruitsRepertory();
				break;
			}
			case 5: {
				addPurchesList();
				break;
			}
			case 6: {
				showTopThree();
				break;
			}
			case 7: {
				appointProfit();
				break;
			}
			default:
				throw new IllegalArgumentException("输入错误，请重新输入 ");
			}
			System.out.println("退出请输入0,否则请输入任意");
			String flag = scanner.nextLine();
			if ("#".equals(flag)) {
				break;
			}
		}
	}

}
